package rpc

import (
	"activity/configs"
	integralPB "activity/rpc/proto/integral"
	userPB "activity/rpc/proto/user"
	"context"
	"google.golang.org/grpc"
	"log"
)

func getRpcConn() (*grpc.ClientConn, error) {
	// 建立连接到gRPC服务
	url := configs.GetRpcUrl()
	return grpc.Dial(url, grpc.WithInsecure())
}

func UsernameLogin(username, password string) (*userPB.RespUserParams, error) {
	conn, err := getRpcConn()
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()

	cli := userPB.NewUserSrvClient(conn)

	return cli.UsernameLogin(context.Background(),
		&userPB.LogRegParams{Username: username, Password: password})
}

func GetCurrentIntegral(uid int) (*integralPB.CurrentIntegral, error) {
	conn, err := getRpcConn()
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()

	cli := integralPB.NewIntegralSrvClient(conn)
	return cli.GetCurrentIntegral(context.Background(), &integralPB.Uid{Uid: int64(uid)})
}

func GetFlowRecord(uid int) (*integralPB.RespFlowRecords, error) {
	conn, err := getRpcConn()
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()

	cli := integralPB.NewIntegralSrvClient(conn)
	return cli.GetFlowRecord(context.Background(), &integralPB.ReqFlowRecordsParams{Uid:int64(uid)})
}
